package org.malred.hotelsystem.activities.admin;

import androidx.annotation.NonNull;

import android.view.MenuItem;

import androidx.appcompat.widget.Toolbar;

import com.google.android.material.bottomnavigation.BottomNavigationView;

import org.malred.hotelsystem.R;
import org.malred.hotelsystem.activities.BaseActivity;
import org.malred.hotelsystem.fragment.admin.ManageAddFragment;
import org.malred.hotelsystem.fragment.admin.ManageHomeFragment;
import org.malred.hotelsystem.fragment.admin.ManageOrderFragment;

public class ManagerActivity extends BaseActivity {
    private static final String TAG = "ManagerActivity";
    Toolbar toolbar;
    //    RecyclerView rv;
//    private List<HotelEntity> hotelEntities;
//    private HotelRecycleAdapter adapter;
    private ManageHomeFragment homeFragment;
    private ManageOrderFragment orderFragment;
    private ManageAddFragment changeFragment;
    private BottomNavigationView bottomNavigationView;

    @Override
    protected int initLayout() {
        return R.layout.activity_manager;
    }

    @Override
    protected void initView() {
//        Object username = this.getIntent().getExtras().get("username");
//        Log.i(TAG, "initView: " + intentExtras.get("username"));
        toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        bottomNavigationView = findViewById(R.id.manage_bottom_nav);
    }

    @Override
    protected void initData() {
        // 默认首页选中
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.manage_content, new ManageHomeFragment()).commit();
        // 切换底部导航事件
        bottomNavigationView.setOnItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                if (item.getItemId() == R.id.home) {
//                    selectedFragment(0);
                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.manage_content, new ManageHomeFragment()).commit();
                    return true;
                } else if (item.getItemId() == R.id.add) {
//                    selectedFragment(1);
                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.manage_content, new ManageAddFragment()).commit();
                    return true;
                } else if (item.getItemId() == R.id.order) {
//                    selectedFragment(2);
                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.manage_content, new ManageOrderFragment()).commit();
                    return true;
                }
                // return true 底部图标才会变
                return false;
            }
        });
    }

//    private void selectedFragment(int position) {
//        // fragment管理器
//        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
//        // 隐藏所有fragment
//        hideFragment(fragmentTransaction);
//        if (position == 0) {
////            if (homeFragment == null) {
//            homeFragment = new ManageHomeFragment();
//            fragmentTransaction.add(R.id.manage_content, homeFragment);
////            } else {
//            fragmentTransaction.show(homeFragment);
////            }
//        } else if (position == 1) {
//            if (changeFragment == null) {
//                changeFragment = new ManageAddFragment();
//                // 显示在xml的fragment里
//                fragmentTransaction.add(R.id.manage_content, changeFragment);
//            } else {
//                // 显示在xml的fragment里
//                fragmentTransaction.show(changeFragment);
//            }
//        } else if (position == 2) {
//            if (orderFragment == null) {
//                orderFragment = new ManageOrderFragment();
//                fragmentTransaction.add(R.id.manage_content, orderFragment);
//            } else {
//                fragmentTransaction.show(orderFragment);
//            }
//        }
//        // 提交
//        fragmentTransaction.commit();
//    }
//
//    private void hideFragment(FragmentTransaction fragmentTransaction) {
//        if (homeFragment != null) {
////            fragmentTransaction.hide(homeFragment);
//            fragmentTransaction.remove(homeFragment);
//        }
//        if (changeFragment != null) {
//            fragmentTransaction.hide(changeFragment);
//        }
//        if (orderFragment != null) {
//            fragmentTransaction.hide(orderFragment);
//        }
//    }
}